package com.hlx.controller.user;

import com.hlx.entity.user.Ticket;
import com.hlx.service.admin.StationService;
import com.hlx.service.admin.TrainNumberService;
import com.hlx.service.user.*;
import com.hlx.utils.Result;
import com.hlx.vo.TicketFormVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ticketFormVo")
public class TikcetFormVoController {
    @Autowired
    private StationService stationService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private TrainNumberService trainNumberService;
    @Autowired
    private TrainNumberStationService trainNumberStationService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("/getAllTicketFormVoByPersonId/{personId}")
    public Result<List<TicketFormVo>> getAllTicketFormVoByPersonId(@PathVariable Integer personId){
        List<Ticket> tickets = ticketService.getTicketByPersonId(personId);
        if(tickets.isEmpty()){
            return Result.error("此人无车票");
        }
        List<TicketFormVo> ticketVos = new ArrayList<>();
        Iterator<Ticket> it = tickets.iterator();
        while (it.hasNext()){
            TicketFormVo ticketVo = new TicketFormVo();
            Ticket ticket = it.next();
            ticketVo.setId(ticket.getId());
            ticketVo.setTrainId(ticket.getTrainId());
            ticketVo.setStartId(ticket.getStartId());
            ticketVo.setArriveId(ticket.getArriveId());
            ticketVo.setPrice(ticket.getPrice());
            ticketVo.setSeatType(ticket.getSeatType());
            ticketVo.setDiscountType(ticket.getDiscountType());
            ticketVo.setPersonId(ticket.getPersonId());
            ticketVo.setStatus(ticket.getStatus());
            ticketVo.setDate(ticket.getDate());
            ticketVo.setTrainNumber(trainNumberService.getById(ticketVo.getTrainId()).getTrainNumberName());
            ticketVo.setStartStation(stationService.getById(ticketVo.getStartId()).getStationName());
            ticketVo.setArriveStation(stationService.getById(ticketVo.getArriveId()).getStationName());
            ticketVo.setCustomerName(customerService.getById(ticketVo.getPersonId()).getCustomerName());
            ticketVo.setStartTime(trainNumberStationService.getTrainNumberByStationNameAndTrainNumber(ticketVo.getStartStation(),ticketVo.getTrainNumber()).getStartTime());
            ticketVo.setArriveTime(trainNumberStationService.getTrainNumberByStationNameAndTrainNumber(ticketVo.getArriveStation(),ticketVo.getTrainNumber()).getArriveTime());
            ticketVos.add(ticketVo);
        }
        log.info("{}",ticketVos);
        return Result.success(ticketVos);
    }
}
